package com.aistar.controller;

import com.aistar.pojo.Shop;
import com.aistar.pojo.ShopVO;
import com.aistar.service.ShopService;
import com.aistar.service.impl.ShopServiceImpl;
import com.aistar.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class ShopController {
    @Autowired
    private ShopService shopService;
    @RequestMapping("shop")
    @ResponseBody
    public  PageInfo<Shop> getall(HttpServletRequest request) {



        //1. 获得url后面页码
        String pageNumStr = request.getParameter("page");
        System.out.println("shop controller pageNum:"+pageNumStr);
        Integer pageNum = null;

        //2. 页码数据类型转换
        if(pageNumStr ==null)
            pageNum = 1;
        else
            pageNum = Integer.valueOf(pageNumStr);



        Shop shop = new Shop();  //封装页面的条件

        if(request.getParameter("name")!=null && !request.getParameter("name").equals("")){
            shop.setShopName(request.getParameter("name"));
        }
        if(request.getParameter("kw")!=null && !request.getParameter("kw").equals("")){
            shop.setKeyword(request.getParameter("kw"));
        }
        if(request.getParameter("area")!=null && !request.getParameter("area").equals("")){
            shop.setShopAddArea(request.getParameter("area"));
        }

        System.out.println("shop:"+shop);

        //3. service 调用

        PageInfo<Shop> pageInfo = shopService.getBySearchWord(shop,pageNum,2);


        return pageInfo;
    }

    @GetMapping("shop/{id}")
    public ModelAndView getById(@PathVariable("id") int shopId){
        System.out.println("getById....");
        ShopVO shopVO = shopService.getById(shopId);
        ModelAndView mav = new ModelAndView();
        mav.addObject("shopvo",shopVO);
        mav.setViewName("shop/info");  // /shop/info.jsp
        return  mav;
    }


    //搜索框中的关键字查询
    @GetMapping("shop/kw/{keyword}")
    //@ResponseBody
    public ModelAndView getBykeyword(@PathVariable("keyword")String   keyword){
        System.out.println("search keyword:"+keyword);
        List<Shop> shopList =   this.shopService.getByKeyWord(keyword);
        ModelAndView mav = new ModelAndView();
        mav.setViewName("shop/info");

        return mav;
    }

    //多条件查询shop
    @GetMapping("shop/{shop_name}/{food_cata}/{addr}")
   // @ResponseBody
    public List<Shop> getByMorekw(@PathVariable("shop_name") String shopName,@PathVariable("food_cata") String foodCata, @PathVariable("addr") String addr){
        Shop shop = new Shop();
        shop.setShopName(shopName);
        shop.setKeyword(foodCata);
        shop.setShopAddArea(addr);
        List<Shop> shopList =   this.shopService.getByMoreKeyword(shop);
        return shopList;
    }


}
